let cookie;
let rawCookie;
let checkBtnIsPressed = false;
let isValidCookie = false;

getAnnouncement()
setInterval("openAdminMode()", 1000)

function refreshConfig() {

    let command = document.getElementById("command").value
    let password = document.getElementById("password").value

    $.ajax({
        url: "jd/refreshConfig", type: "POST", dataType: "json", data: {
            command: command, password: password
        }, success: function (data) {
            alert(data.msg)
        }, error: function (data) {
            alert(data.status)
            console.log(data)
        }
    });
}

function getAnnouncement() {
    $.ajax({
        url: "jd/announcement", type: "GET", dataType: "json", success: function (data) {
            if (data.data !== "默认公告") {
                alert(data.data)
            }
        }, error: function (data) {
            alert(data.status)
            console.log(data)
        }
    });
}

function quitAdminMode() {
    let nickname_ele = document.getElementById("nickname")
    nickname_ele.value = "关闭管理员模式";
}

function openAdminMode() {
    let nickname_ele = document.getElementById("nickname")
    if (nickname_ele.value === "管理员模式已开启" || nickname_ele.value === "") {
        return;
    }
    if (nickname_ele.value === "关闭管理员模式") {
        nickname_ele.value = ""
        $("#div_refresh_config").css('display', 'none');
        alert("管理员模式已关闭")
        return;
    }
    if (nickname_ele.value === "admin") {
        nickname_ele.value = "管理员模式已开启"
        $("#div_refresh_config").show();
        alert("管理员模式已开启")
    }
}

function jumpToTutorial() {
    window.open("https://docs.qq.com/doc/DVUxwTEJBd2VaWE1F")
}

function checkCookie() {
    checkBtnIsPressed = true;
    rawCookie = document.getElementById("cookie").value;
    let pt_key = rawCookie.match(/pt_key=.+?;/);
    let pt_pin = rawCookie.match(/pt_pin=.+?;/);

    if (rawCookie === "cookie") {
        cookie = "pt_key=123;pt_pin=456;"
        isValidCookie = true;
        return;
    }

    if (pt_key != null && pt_pin != null) {
        cookie = pt_key + pt_pin;
        isValidCookie = true;
        alert("成功获取到cookie\n" + cookie)
    } else {
        alert("没有获取到cookie,请检查获取步骤是否正确")
    }
}

function feedback() {

    let nickname = document.getElementById("nickname_1").value;
    let email = document.getElementById("email").value;
    let content = document.getElementById("content").value;

    if (nickname == null || nickname === "") {
        alert("请先填写昵称")
        return;
    }
    if (email == null || email === "" || content == null || content === "") {
        alert("请填写邮箱或者反馈内容")
        return;
    }

    $.ajax({
        url: "jd/feedback", type: "POST", dataType: "json", data: {
            nickname: nickname, content: content, email: email
        }, success: function (data) {
            alert(data.msg)
        }, error: function (data) {
            alert(data.status)
            console.log(data)
        }
    });
}

function submitData() {

    if (!checkBtnIsPressed) {
        alert("请先点击[检测cookie]按钮");
        return;
    }

    checkBtnIsPressed = false;

    if (!isValidCookie) {
        alert("没有获取到cookie,请检查获取步骤是否正确");
        return;
    }

    isValidCookie = false;

    rawCookie = document.getElementById("cookie").value;

    let nickname = document.getElementById("nickname").value;
    let wxPusherId = document.getElementById("wxPusherId").value.match(/UID_[0-9a-zA-Z]+/)[0];

    if (nickname == null || nickname === "" || wxPusherId == null || wxPusherId === "" || rawCookie == null || rawCookie === "") {
        alert("存在必填项未填,请检查")
        return;
    }

    let jdUser = {nickname: nickname, cookie: cookie, wxPusherId: wxPusherId}

    if (confirm("请先确认你要提交的信息" + "\n昵称:  " + nickname + "\ncookie:  " + cookie + "\nUID:  " + wxPusherId + "\n是否提交?")) {
        console.log(jdUser)
        $.ajax({
            url: "jd/submitData", type: "POST", dataType: "json", data: jdUser, success: function (data) {
                alert(data.msg)
            }, error: function (data) {
                alert(data.error)
                console.log(data)
            }
        });

    } else {
        alert("取消提交")
    }


}
